package com.cloudspace.yunloan.net.manager;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.signature.StringSignature;
import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.utils.ImageUtil;

import java.io.File;

public class ImageManager {

    public enum ImageRadius {
        ROUND(360), /* 圆形图片 */
        RADIUS_3(3), /* Radius=3的圆角矩形 */
        RADIUS_7(7), /* Radius=7的圆角矩形 */
        RADIUS_8(8), /* Radius=8的圆角矩形 */
        RADIUS_10(10); /* Radius=10的圆角矩形 */

        private int mRadius;

        ImageRadius(int radius) {
            this.mRadius = radius;
        }

        public int getRadius() {
            return this.mRadius;
        }
    }


    public static void loadCircleImage(final Context context, ImageView imageView, String url) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        // 小图标
        Glide.with(context.getApplicationContext()).load(url).asBitmap().centerCrop().into(new RoundBitmapImageViewTarget(imageView));
    }


    public static void loadCircleImage(final Context context, ImageView imageView,String url, int radius) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        // 小图标
        Glide.with(context.getApplicationContext()).load(url).asBitmap().centerCrop().into(new RoundBitmapImageViewTarget(imageView, radius));
    }

    public static void loadCircleImage(final Context context, ImageView imageView, String url, @DrawableRes int placeHolder, final int radius){
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        // 小图标
        Glide.with(context.getApplicationContext()).load(url).asBitmap().centerCrop().placeholder(placeHolder).into(new RoundBitmapImageViewTarget(imageView,radius));
    }

    static class RoundBitmapImageViewTarget extends BitmapImageViewTarget{
        private int radius = 0;

        public RoundBitmapImageViewTarget(ImageView view) {
            super(view);
        }

        public RoundBitmapImageViewTarget(ImageView view, int radius){
            super(view);
            if (radius > 0){
                this.radius = radius;
            }
        }

        @Override
        protected void setResource(Bitmap resource) {
            if (getView() == null || getView().getContext() == null){
                return;
            }

            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(getView().getContext().getResources(), resource);
            circularBitmapDrawable.setCircular(true);

            if (radius > 0){
                circularBitmapDrawable.setCornerRadius(radius);
            }

            getView().setImageDrawable(circularBitmapDrawable);
        }
    }

    public static void loadCircleImage(final Context context, final ImageView imageView, String url, final String path) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        Glide.with(context.getApplicationContext()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                ImageUtil.saveBitmap(resource, path, Bitmap.CompressFormat.JPEG, 100);
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    public static void loadCircleImage(Context context, ImageView imageView, File filepath) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        Glide.with(context.getApplicationContext()).load(filepath).asBitmap().centerCrop().into(new RoundBitmapImageViewTarget(imageView));
    }



    public static void loadImage(Context context, String url, SimpleTarget<Bitmap> imageCallbck) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        Glide.with(context.getApplicationContext()).load(url).asBitmap().into(imageCallbck);
    }

    public static void pauseLoad(Context context) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        clearMemory(context.getApplicationContext());

        Glide.with(context.getApplicationContext()).pauseRequests();
    }

    public static void clearMemory(Context context){
        if (null == context){
            return;
        }

        Glide.get(context).clearMemory();
    }

    public static void resumeLoad(Context context) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        Glide.with(context.getApplicationContext()).resumeRequests();
    }

    public static void loadImage(final Context context, ImageView imageView, String url, int placeHolder) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }
        Glide.with(context).load(url).asBitmap().centerCrop().placeholder(placeHolder).into(imageView);
    }

    public static void loadImage4Banner(Context context, ImageView imageView, String url) {
        if (null == context ){
            return;
        }
        Glide.with(context).load(url).asBitmap().centerCrop().placeholder(R.drawable.default_head).into(imageView);

    }

    public static void loadImage(Context context, ImageView imageView, String filepath) {
        if (null == context || context.getApplicationContext() == null){
            return;
        }

        StringSignature signature = new StringSignature("" + System.currentTimeMillis());
        Glide.with(context.getApplicationContext()).load(filepath).signature(signature).into(imageView);
    }

}
